﻿using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using SuperSocket;
using SuperSocket.Command;
using SuperSocket.ProtoBase;
using SuperSocket.Server;
using SuperSocketV2Sample.MultipleServer.Commands;
using SuperSocketV2Sample.MultipleServer.Server;

namespace SuperSocketV2Sample.MultipleServer
{
    class Program
    {
        static async Task Main()
        {
            var host = MultipleServerHostBuilder.Create()
                .AddServer<TelnetServiceA<StringPackageInfo>, StringPackageInfo, CommandLinePipelineFilter>(builder =>
                {
                    builder.ConfigureServerOptions((ctx, config) =>
                        {
                            //获取服务配置
                            // ReSharper disable once ConvertToLambdaExpression
                            return config.GetSection("TelnetServerA");
                        })
                        .UseSession<TelnetSessionA>()
                        .UseCommand(commandOptions =>
                        {
                            commandOptions.AddCommand<AddCommand>();
                            commandOptions.AddCommand<DivCommand>();
                            commandOptions.AddCommand<EchoCommand>();
                        });
                })
                .AddServer<TelnetServiceB<StringPackageInfo>, StringPackageInfo, CommandLinePipelineFilter>(builder =>
                {
                    builder.ConfigureServerOptions((ctx, config) =>
                        {
                            //获取服务配置
                            // ReSharper disable once ConvertToLambdaExpression
                            return config.GetSection("TelnetServerB");
                        })
                        .UseSession<TelnetSessionB>()
                        .UseCommand(commandOptions =>
                        {
                            commandOptions.AddCommand<MulCommand>();
                            commandOptions.AddCommand<SubCommand>();
                            commandOptions.AddCommand<EchoCommand>();
                        });
                })
                //配置日志
                .ConfigureLogging((hostCtx, loggingBuilder) =>
                {
                    //添加控制台输出
                    loggingBuilder.AddConsole();
                })
                .Build();
            try
            {
                await host.RunAsync();
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}
